﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevTeam;
using System;
using System.Data;
using System.IO;
using System.Net;
using System.Xml;

namespace ConfExpert.Forms.Depo.Kargo
{
	public partial class DHLWS : XForm
	{
		public DHLWS()
		{
			InitializeComponent();
		}

		MyWebBrowser webBrowser1;
		private void DHLWS_Load(object sender, EventArgs e)
		{
			btnGetir.ItemClick += btnGetir_ItemClick;

			this.webBrowser1 = new MyWebBrowser();
			this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.webBrowser1.Location = new System.Drawing.Point(0, 24);
			//this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
			this.webBrowser1.Name = "webBrowser1";
			//this.webBrowser1.Size = new System.Drawing.Size(284, 241);
			this.webBrowser1.TabIndex = 4;
			this.Controls.Add(this.webBrowser1);
			this.Controls.SetChildIndex(this.webBrowser1, 0);

			dt = new DataTable();
			dt.Columns.Add("WayBillNo", typeof(string));
			dt.Columns.Add("ActionStatus", typeof(string));
		}

		void btnGetir_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
		{
			if (txtWayBillNo.EditValue != null)
				Fetch(txtWayBillNo.EditValue.ToString());
			else
			{
				dt.Clear();

				DataTable t = ExecuteDataTable("GetAWB");
				foreach (DataRow r in t.Rows)
					Fetch(r["WayBillNo"].ToStr());

				ExecuteNonQuery("UpdAWBStatus", dt.Serialize());
			}
		}

		DataTable dt;
		void Fetch(string WayBillNo)
		{
			string requestText = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<req:KnownTrackingRequest xmlns:req=""http://www.dhl.com"" 
	xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
	xsi:schemaLocation=""http://www.dhl.com
	TrackingRequest.xsd"">
	<Request>
		<ServiceHeader>
			<MessageTime>2016-01-17T14:00:00+02:00</MessageTime>
			<MessageReference>12345678912345678912345678912</MessageReference>
			<SiteID>!usr</SiteID>
			<Password>!pwd</Password>
		</ServiceHeader>
	</Request>
	<LanguageCode>en</LanguageCode>
	<AWBNumber>!awb</AWBNumber>
	<LevelOfDetails>LAST_CHECK_POINT_ONLY</LevelOfDetails>
	<!--LevelOfDetails>ALL_CHECK_POINTS</LevelOfDetails-->
</req:KnownTrackingRequest>".Replace("!awb", WayBillNo).Replace("!usr", AppSession.DhlUsername).Replace("!pwd", AppSession.DhlPassword);
			//Örnek AWB: 8117928906

			WebRequest requestRate = HttpWebRequest.Create("https://xmlpi-ea.dhl.com/XMLShippingServlet");
			requestRate.ContentType = "application/x-www-form-urlencoded";
			requestRate.Method = "POST";

			using (var stream = requestRate.GetRequestStream())
			{
				var arrBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(requestText);
				stream.Write(arrBytes, 0, arrBytes.Length);
				stream.Close();
			}

			WebResponse responseRate = requestRate.GetResponse();
			var respStream = responseRate.GetResponseStream();

			var reader = new StreamReader(respStream, System.Text.Encoding.ASCII);
			var xmlDoc = new XmlDocument();
			xmlDoc.Load(reader);
			webBrowser1.DocumentXml = xmlDoc;
			respStream.Close();


			string xml = xmlDoc.InnerXml;
			int start = xml.IndexOf("<ActionStatus>");
			if (start > 0)
			{
				string res = xml.Substring(start + 14, xml.IndexOf("</ActionStatus>", start) - start - 14);
				dt.Rows.Add(WayBillNo, res);
			}

			/*
			DHL.gblDHLExpressTrackClient client = new DHL.gblDHLExpressTrackClient();
			client.Open();
			string userName = AppSession.UnspedUsername;
			string password = AppSession.UnspedPassword;

			BasicHttpBinding binding = (BasicHttpBinding)client.Endpoint.Binding;
			binding.MaxBufferSize = Int32.MaxValue;
			binding.MaxReceivedMessageSize = Int32.MaxValue;

		
			//DHL.TrackingRequest req = new DHL.TrackingRequest();
			//req.AWBNumber = new string[] {"686876876"};
			//req.LevelOfDetails = DHL.LevelOfDetails.ALL_CHECK_POINTS;
			//DHL.pubTrackingRequest req2 = new DHL.pubTrackingRequest();
			//req2.TrackingRequest = req;
			//DHL.pubTrackingResponse res = dhl.trackShipmentRequest(req2);
			

			pubTrackingRequest req = new pubTrackingRequest();
			pubTrackingResponse res = new pubTrackingResponse();
			client.ClientCredentials.UserName.UserName = "KARBEL";
			client.ClientCredentials.UserName.Password = "mACsaYzLp_";
			req.TrackingRequest = new TrackingRequest();
			req.TrackingRequest.AWBNumber = new string[] { "8652195554" };
			res = client.trackShipmentRequest(req); //exception trown
			*/
		}




	}
}
